package com.shimao.celnet.payment.controller;


import com.shimao.celnet.payment.config.ConstantConfig;
import com.shimao.celnet.payment.model.PPay;
import com.shimao.celnet.payment.model.PPayCUPUser;
import com.shimao.celnet.payment.model.PPayCust;
import com.shimao.celnet.payment.service.PPayCupUserService;
import com.shimao.celnet.payment.service.PPayCustService;
import com.shimao.celnet.payment.service.PPayService;
import com.shimao.celnet.payment.service.impl.apiservice.CallOut_TLPublicService;
import com.shimao.celnet.payment.util.FinalUtil;
import com.shimao.celnet.payment.util.MyDateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by XSW on 2018/10/29.
 * 银联支付控制类
 */
@Controller
@RequestMapping(value = "payment")
public class PayCUPController {
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private PPayCupUserService pPayCupUserService;

    @Autowired
    private PPayService pPayService;

    @Autowired
    private CallOut_TLPublicService callOut_tlPublicService;

    @Autowired
    private PPayCustService pPayCustService;

    @Autowired
    private ConstantConfig constantConfig;


    @RequestMapping(value ="payCupRegPage",method = RequestMethod.GET)//跳转申请签约页面
    public String cupRegPage(){
        return "cupPay/unionPay";
    }


    @RequestMapping(value ="confirmRegPage",method = RequestMethod.GET)//跳转确认申请签约页面
    public String cupConfirmRegPage(Model model, HttpServletRequest req, HttpServletResponse res){
        String cupguid = req.getParameter("cupguid");
        model.addAttribute("cupGuid",cupguid);
        return "cupPay/signedSMS";
    }

    @RequestMapping(value ="paySMSPage",method = RequestMethod.GET)//跳转确认申请支付页面
    public String paySMSPage(Model model, HttpServletRequest req, HttpServletResponse res){
        String cupguid = req.getParameter("cupguid");
        model.addAttribute("cupGuid",cupguid);
        return "cupPay/paySMS";
    }

    @RequestMapping(value ="payCupPayPage",method = RequestMethod.GET)//跳转确认申请支付页面
    public String payCupPayPage(Model model, HttpServletRequest req, HttpServletResponse res){
        String paymentId = req.getParameter("paymentId");
        PPay pPay = pPayService.getByKey(paymentId);
        String agreeid = req.getParameter("agreeid");
        PPayCUPUser payCUPUser = pPayCupUserService.findCUPUserByMeruseridAndAgreeid(pPay.getPayUserSfGuid(),agreeid);
        payCUPUser.setPayPaymentId(paymentId);
        //根据小区sfid获取商户信息
        PPayCust pPayCust = getTLMessage(pPay.getEstateGuid(), FinalUtil.CUP_PAY_TYPE);
        try {
            //请求申请支付确认签约接口
            logger.info("==================请求通联申请支付开始=====================");
            logger.info("========订单号："+pPay.getGuid() +"=========商户号："+pPayCust.getCustId());
            Map<String, String>  mapPay = callOut_tlPublicService.payapply(MyDateUtil.NotString(pPay.getGuid()),payCUPUser.getPayAgreeid(),pPay.getPayAmount(),pPay.getBusinessType(),pPay.getPayRemark(),constantConfig.getTlReturnURL(),pPayCust);
            logger.info("========通联响应参数："+mapPay.toString());
            if("SUCCESS".equals(mapPay.get("retcode"))&&"0000".equals(mapPay.get("trxstatus"))){ //交易成功,交易流程完成,无需支付确认
                pPay.setTrxid(mapPay.get("trxid"));
                pPayService.update(pPay);
                return "payment/paymentSuccess";
            }else if("SUCCESS".equals(mapPay.get("retcode"))&&"1999".equals(mapPay.get("trxstatus"))){//短信验证码已发送,请继续调用支付确认接口完成支付

                if(mapPay.get("thpinfo") != null && mapPay.get("thpinfo") != ""){
                    payCUPUser.setPayThpinfo(mapPay.get("thpinfo"));
                    pPayCupUserService.update(payCUPUser);
                }
                model.addAttribute("cupGuid",payCUPUser.getGuid());
                return "cupPay/paySMS";

            }else if("SUCCESS".equals(mapPay.get("retcode"))&&"2008".equals(mapPay.get("trxstatus"))){//交易处理中,请查询交易   跳转支付等待页面，进行查询交易
                pPayCupUserService.update(payCUPUser);
                return "payment/paymentWait";

            }else if("SUCCESS".equals(mapPay.get("retcode"))&&"2000".equals(mapPay.get("trxstatus"))){ //交易处理中,请查询交易  跳转支付等待页面，进行查询交易
                pPayCupUserService.update(payCUPUser);
                return "payment/paymentWait";
            }else {                     //交易失败  跳转支付失败
                return "payment/paymentFail";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "payment/paymentFail";  //交易失败  跳转支付失败
    }


    @RequestMapping(value ="bankCardDetails",method = RequestMethod.GET)//跳转银行卡详情页面
    public String bankCardDetailsPage(Model model, HttpServletRequest req, HttpServletResponse res){
        String cupguid = req.getParameter("cupguid");
        PPayCUPUser payCUPUser = pPayCupUserService.getByKey(cupguid);
        Map<String,String> map = getBankQuota(payCUPUser.getPayBankname(),payCUPUser.getPayAccttype());
        payCUPUser.setPayAcctno(MyDateUtil.hideCardNo(payCUPUser.getPayAcctno()));
        model.addAttribute("bank",payCUPUser);
        model.addAttribute("oneExpenditure",map.get("oneExpenditure"));
        model.addAttribute("DayExpenditure",map.get("DayExpenditure"));
        return "cupPay/unbindCard";
    }

    @RequestMapping(value ="bindBankCardSuccess",method = RequestMethod.GET)//跳转解绑银行卡成功页面
    public String bindBankCardSuccessPage(Model model, HttpServletRequest req, HttpServletResponse res){

        return "cupPay/bindBankCardSuccess";
    }

    @RequestMapping(value ="bindBankCardFail",method = RequestMethod.GET)//跳转解绑银行卡失败页面
    public String bindBankCardFailPage(Model model, HttpServletRequest req, HttpServletResponse res){
        return "cupPay/bindBankCardFail";
    }

    @RequestMapping(value ="unbindBankCardPage",method = RequestMethod.GET)//跳转解绑银行卡页面
    public String unbindBankCardPage(Model model, HttpServletRequest req, HttpServletResponse res) {
        String cupguid = req.getParameter("cupguid");
        PPayCUPUser pPayCUPUser = pPayCupUserService.getByKey(cupguid);
        List<PPayCUPUser> pPayCUPUsers = pPayCupUserService.findCUPUserByMeruserid(pPayCUPUser.getPayMeruserid(), pPayCUPUser.getPayCustid());
        if (pPayCUPUsers.size() > 0 && pPayCUPUsers != null) {
            if (pPayCUPUsers != null && pPayCUPUsers.size() > 0) {
                for (PPayCUPUser p :
                        pPayCUPUsers) {
                    p.setPayAcctno(MyDateUtil.hideCardNo(p.getPayAcctno()));
                }
                model.addAttribute("banks", pPayCUPUsers);
                model.addAttribute("cupguid", pPayCUPUsers.get(0).getGuid());
            }
        }
            return "cupPay/bindCard";
    }

    /**
     * 根据小区sfid查询通联账户信息
     * @param estateGuid
     * @return
     */
    public PPayCust getTLMessage(String estateGuid ,String payType){
        return pPayCustService.getBySfGuid1(estateGuid,payType);
    }

    /**
     *
     * @Author Galen
     * @Description //TODO 通过银行的名字和银行卡类型获取银行卡的限额
     * @Date 10:15 2018/11/5
     * @Param bankName
     * @return
     */
    public Map<String,String> getBankQuota(String bankName,String bankType){
        Map<String,String> bankmap = new TreeMap<>();
        switch(bankName){
            case "工商银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }
                break;
            case "农业银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","10,000");
                    bankmap.put("oneExpenditure","2,000");
                }else {
                    bankmap.put("DayExpenditure","No limit");
                    bankmap.put("oneExpenditure","No limit");
                } break;
            case "中国银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","No limit");
                    bankmap.put("oneExpenditure","No limit");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }
                break;
            case "建设银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }
                break;
            case "交通银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }
                break;
            case "兴业银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                 }else {
                    bankmap.put("DayExpenditure","20,000");
                    bankmap.put("oneExpenditure","20,000");
                  }
                break;
            case "中信银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }else {
                    bankmap.put("DayExpenditure","No limit");
                    bankmap.put("oneExpenditure","No limit");
                }
                break;
            case "平安银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","20,000");
                    bankmap.put("oneExpenditure","20,000");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","20,000");
                }
                break;
            case "光大银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","20,000");
                }
                break;
            case "浦发银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","20,000");
                }
                break;
            case "招商银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","5,000");
                    bankmap.put("oneExpenditure","5,000");
                }else {
                    bankmap.put("DayExpenditure","No limit");
                    bankmap.put("oneExpenditure","No limit");
                }
                break;
            case "民生银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","10,000");
                }
                break;
            case "华夏银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","2,000");
                    bankmap.put("oneExpenditure","2,000");
                }else {
                    bankmap.put("DayExpenditure","No limit");
                    bankmap.put("oneExpenditure","No limit");
                }
                break;
            case "广发银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","50,000");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","20,000");
                }
                break;
            case "邮储银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","No limit");
                    bankmap.put("oneExpenditure","No limit");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","20,000");
                }
                break;
            case "北京银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","5,000");
                    bankmap.put("oneExpenditure","5,000");
                }else {
                    bankmap.put("DayExpenditure","No limit");
                    bankmap.put("oneExpenditure","No limit");
                }
                break;
            case "上海银行":
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","5,000");
                    bankmap.put("oneExpenditure","5,000");
                }else {
                    bankmap.put("DayExpenditure","No limit");
                    bankmap.put("oneExpenditure","No limit");
                }
                break;
            default:
                if("00".equals(bankType)){
                    bankmap.put("DayExpenditure","5,000");
                    bankmap.put("oneExpenditure","5,000");
                }else {
                    bankmap.put("DayExpenditure","50,000");
                    bankmap.put("oneExpenditure","20,000");
                }
                break;
        }

        return bankmap;
    }


}
